package com.zw.yzk.kabu.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;

import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseActivity;
import com.zw.yzk.kabu.data.utils.SharedPrefrenceUtils;
import com.zw.yzk.kabu.domain.interactor.DefaultOnNext;
import com.zw.yzk.kabu.internal.component.DaggerSplashComponent;
import com.zw.yzk.kabu.internal.module.ActivityModule;
import com.zw.yzk.kabu.internal.module.SplashModule;
import com.zw.yzk.kabu.presenter.SplashPresenter;
import com.zw.yzk.kabu.utils.PhoneUtils;
import com.zw.yzk.kabu.view.view.SplashView;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.Bind;
import butterknife.OnClick;
import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;

import static rx.Notification.Kind.OnNext;

/**
 * Created by zhanwei on 2016/12/31.
 */

public class SplashActivity extends BaseActivity implements SplashView {

    @Bind(R.id.splash)
    ImageView splash;
    @Bind(R.id.skip)
    TextView skip;

    @Inject
    SplashPresenter presenter;
    private Subscription subscription;


    @Override
    public int getLayoutId() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        return R.layout.activity_splash;
    }

    @Override
    public void iniToolbar(BaseActivity activity) {

    }

    @Override
    public void initComponent() {
        DaggerSplashComponent
                .builder()
                .applicationComponent(getApplicationComponent())
                .activityModule(new ActivityModule(this))
                .splashModule(new SplashModule())
                .build()
                .inject(this);
        presenter.setView(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSplashImage();
        presenter.checkLoginStatus();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
        }
        if (subscription != null) {
            subscription.unsubscribe();
        }
    }

    @Override
    public void loginStatus(boolean login) {
        subscription = Observable.interval(0, 1, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .onBackpressureDrop()
                .subscribe(new DefaultOnNext<Long>() {
                    @Override
                    public void call(Long aLong) {
                        if (aLong == 5) {
                            startActivity(new Intent(SplashActivity.this, MainActivity.class));
                            finish();
                        }
                        skip.setText(getResources().getString(R.string.skip_add, 5 - aLong));
                    }
                });
    }

    @Override
    public void getSplashSuccess(String image) {
        loadSplashImage(image);
    }

    @Override
    protected boolean skinStatusBarColorEnable() {
        return false;
    }


    @OnClick(R.id.skip)
    public void skip() {
        startActivity(new Intent(SplashActivity.this, MainActivity.class));
        finish();
    }

    /**
     * 加载欢迎页
     */
    private void getSplashImage() {
        loadSplashImage(SharedPrefrenceUtils.getString(this, SharedPrefrenceUtils.getSplashImageKey()));
        presenter.getSplashImage();
    }

    private void loadSplashImage(String url) {
        Glide.with(this)
                .load(url)
                .centerCrop()
                .placeholder(R.drawable.im_spalsh)
                .error(R.drawable.im_spalsh)
                .into(splash);
    }
}
